import {createStore} from 'vuex';
export default createStore({
    state:{
        cartList:[],//购物车列表
        orderList:[],//生成订单列表
        orderListed:[],//生成订单后列表
        edit:true,//编辑的状态
        userAddress: [
            {
              id: 1001,
              name: '大钊',
              tel: '13123456731',
              province: '广东省',
              city: '广州市',
              county: '天河区',
              addressDetail: 'asd',
              isDefault: true,
              areaCode: '110101',
            },
            {
              id: 1002,
              name: '阿钊',
              tel: '13123456731',
              province: '广东省',
              city: '广州市',
              county: '天河区',
              addressDetail: 'asd',
              isDefault: false,
              areaCode: '110101',
            },
          ],
    },
    mutations:{
        ADDCART(state,value){
            state.cartList=value;
        },
        PAY(state,value){
            state.orderList=value;
        },
        DELETE(state,value){
            state.cartList=value;
        },
        UPDAETORDER(state){
            state.orderListed=state.orderListed.concat(state.orderList);
        },
        EDIT(state,value){
            if(value==='delete'){
                state.edit=true;
            }
            else{
                state.edit=!state.edit;
            }
        },

        //新增地址
        ADDADDRESS(state,value){
            state.userAddress.map((item)=>{
                if(value.isDefault){
                    item.isDefault=false;
                }
            });
            state.userAddress.push(value);
        },

        //编辑地址
        CHANGEADDRESS(state,value){
            state.userAddress=state.userAddress.map((item)=>{
                if(value.isDefault){
                    item.isDefault=false;
                }
                return item.id===value.id ? value : item;
            });
        },
        //删除地址按钮
        DELETEADDRESS(state, value) {
            state.userAddress = state.userAddress.filter((item) => {
              return !(value.id === item.id);
            });
            if (value.isDefault && state.userAddress.length) {
              state.userAddress[0].isDefault = true;
            }
          },
        
    },
    actions:{
        
    },
  
    
});